package butines.core.gui;
/*
import static java.awt.event.KeyEvent.VK_BACK_SPACE;
import static java.awt.event.KeyEvent.VK_DELETE;
import static java.awt.event.KeyEvent.VK_END;
import static java.awt.event.KeyEvent.VK_ENTER;
import static java.awt.event.KeyEvent.VK_ESCAPE;
import static java.awt.event.KeyEvent.VK_HOME;
import static java.awt.event.KeyEvent.VK_LEFT;
import static java.awt.event.KeyEvent.VK_RIGHT;

import java.awt.Graphics2D;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import butines.core.GameFont;
import butines.core.Mouse;

public class GUIFieldText extends GUIComponent implements KeyListener {
	
	public static final int MAX_INPUT_LENGTH = 60;

	protected int offset = 0;

	private int maxLength = MAX_INPUT_LENGTH;
	private int size = MAX_INPUT_LENGTH;
	
	private StringBuilder text = new StringBuilder();

	private GameFont font;
	private int x;
	private int y;

	public GUIFieldText(GameFont font, int x, int y) {
		this.x = x;
		this.y = y;
		this.font = font;
	}

	public String getText() {
		return text.toString();
	}
	
	public void setText(String str) {
		if (str == null) {
			text = new StringBuilder();
			offset = 0;
		} else {
			text = new StringBuilder(str);
			offset = text.length();
		}
	}
	
	public void setFont(GameFont font) {
		this.font = font;
	}
	
	public GameFont getFont() {
		return font;
	}

	public void clear() {
		text.setLength(0);
		offset = 0;
	}
	
	public int getMaxLength() {
		return maxLength;
	}

	public void setMaxLength(int maxLength) {
		this.maxLength = maxLength;
	}

	public int getSize() {
		return size;
	}

	public void setSize(int size) {
		this.size = size;
	}

	public int getX() {
		return x;
	}

	public void setX(int x) {
		this.x = x;
	}

	public int getY() {
		return y;
	}

	public void setY(int y) {
		this.y = y;
	}

	private boolean showCursor() {
		return ((int) ((System.currentTimeMillis() / 300)) & 1) == 1;
	}
	
	@Override	
	public void renderComponent(Graphics2D g2d) {
		
		font.render(g2d, x, y, getText());
		
		if (showCursor()) {
			String str = text.substring(0, offset);
			int w = font.getWidth(str);
			font.render(g2d, x + w, y, "|");
		}

	}

	@Override
	public void updateComponent(Mouse mouse) {
		
	}
	
	// implements KeyListener
	@Override
	public void keyTyped(KeyEvent e) {
		char ch = e.getKeyChar();

		switch (ch) {

			case VK_BACK_SPACE:
				if (text.length() > 0) {
					offset--;
					text.deleteCharAt(offset);
				}
				return;

			case VK_DELETE:
				if (offset < text.length()) {
					text.deleteCharAt(offset);
				}
				return;

			case VK_ENTER:
				return;
		}

		if (font.canDisplay(ch) && text.length() < maxLength) {
			if (ch == '\t') {
				text.insert(offset, "    ");
				offset += 4; 
			} else {
				text.insert(offset, ch);
				offset++;
			}
		}

	}

	@Override
	public void keyPressed(KeyEvent e) {
		int code = e.getKeyCode();

		switch (code) {

			case VK_HOME:
				offset = 0;
			break;

			case VK_END:
				offset = text.length();
			break;

			case VK_LEFT:
				if (offset > 0) {
					offset--;
				}
			break;

			case VK_RIGHT:
				if (offset < text.length()) {
					offset++;
				}
			break;
				
			case VK_ESCAPE:
				clear();
			break;

		}

	}

	@Override
	public void keyReleased(KeyEvent e) {

	}

}*/
